<?php
namespace app\admin\controller;

use app\common\controller\Back;

class Cache extends Back
{
    protected function initialize()
    {
        parent::initialize();
    }
    
    public function index()
    {
        $form = xfFormCreate([
            'name'         => 'adminCacheIndex',
            'action'       => xfUrl('admin/cache/update'),
            'items'        => model('admin/Cache','form')->items(config('cache.')),
            'reset'        => '重置',
            'class_button' => 'text-left',
        ]);

        $this->assign('form',$form);

        $this->assign($this->site);

        return $this->fetch();
    }

    public function update()
    {
        $post = xfArrayFilter(input('post.'), model('admin/Cache','form')->keys());
        $post['expire'] = $this->expireValue($post['expire']);
        $post['expire_detail'] = $this->expireValue($post['expire_detail']);
        $post['expire_items'] = $this->expireValue($post['expire_items']);
        $this->configSave('./config/cache.php', $post, 'admin/cache/index');
    }

    private function expireValue($value='')
    {
        if(!is_numeric($value)){
            return false;
        }
        if($value >= 0){
            return intval($value);
        }
        return false;
    }
}